﻿namespace NTLib.Pipe.Targets
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using Descriptors;
    using System.Collections.ObjectModel;

    /// <summary>
    /// Used to generate all the target definition for the local instances
    /// </summary>
    public class LocalTargetProvider : ITargetProvider
    {
        #region Fields

        private static readonly ReadOnlyCollection<Type> s_supportedTypes;

        #endregion

        #region Ctor

        /// <summary>
        /// Initializes the <see cref="LocalTargetProvider"/> class.
        /// </summary>
        static LocalTargetProvider()
        {
            s_supportedTypes = new ReadOnlyCollection<Type>(new Type[] { typeof(LocalTargetLocation) });
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LocalTargetProvider"/> class.
        /// </summary>
        internal LocalTargetProvider()
        {
            this.Id = Guid.NewGuid();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LocalTargetProvider"/> class.
        /// </summary>
        protected LocalTargetProvider(Guid Id)
        {
            this.Id = Id;
        }


        #endregion

        #region Properties

        /// <summary>
        /// Gets the unique identifier of the component.
        /// </summary>
        public Guid Id { get; }

        /// <summary>
        /// Gets the information type managed.
        /// The child type will be associate.
        /// This collection will be used to associate a provider to the information give.
        /// </summary>
        public IEnumerable<Type> InfoTypeManaged
        {
            get { return s_supportedTypes; }
        }

        /// <summary>
        /// Gets a value indicating whether this instance is enabled.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is enabled; otherwise, <c>false</c>.
        /// </value>
        public bool IsEnabled
        {
            get { return true; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Locates the target using the info give in parameters and return all the possible ways to achived to the target.
        /// </summary>
        /// <typeparam name="U">Type of the information container.</typeparam>
        /// <param name="information"></param>
        /// <param name="token"></param>
        /// <returns>
        /// All the possible way to reach the target.
        /// </returns>
        public Task<IEnumerable<ITargetDescriptor>> LocateAsync<U>(U information, CancellationToken token = default(CancellationToken))
        {
            LocalTargetLocation location = information as LocalTargetLocation;
            if (location == null)
                return null;


            return Task.FromResult<IEnumerable<ITargetDescriptor>>(new ITargetDescriptor[] { LocalTargetDescription.Instance });
        }

        #endregion
    }
}
